package sjtu.ist.warehouse4ws.epc2bpmn;

import java.io.File;

import java.util.HashMap;
import java.util.Properties;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import sjtu.ist.warehouse4ws.model.Epcunits;
import sjtu.ist.warehouse4ws.tool.GenerateWsdl;








public class GenerateBpmn {
	public HashMap<String,String> fs;

	public HashMap<String, String> getFs() {
		return fs;
	}

	public void setFs(HashMap<String, String> fs) {
		this.fs = fs;
	}
	public  String writeBpmn(String outFile,Epcunits list) throws Exception{
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		Document doc=db.newDocument();
		
		Epcunits sourceDatas=new Epcunits();
		sourceDatas.setEpcUnits(list.getEpcUnits());
		
		
		WriteUtil writeUtil=new WriteUtil(doc,sourceDatas);
		
		writeUtil.write();
	
		return domDocToFile(doc,outFile,"UTF-8");	
	}
	
	
	
	public static String domDocToFile(Document doc, String fileName,String encoding) throws TransformerException{
		TransformerFactory tfactory=TransformerFactory.newInstance();
		Transformer transformer=tfactory.newTransformer();
		Properties properties=transformer.getOutputProperties();
		properties.setProperty(OutputKeys.ENCODING, encoding);
		properties.setProperty(OutputKeys.METHOD, "xml");
		properties.setProperty(OutputKeys.INDENT, "yes");            //control line
		transformer.setOutputProperties(properties);
		
		DOMSource source=new DOMSource(doc);
		File file=new File(fileName);
		StreamResult result=new StreamResult(file);
		
		transformer.transform(source, result);

		return file.getAbsolutePath();
	}

	public static void main(String[] args) throws Exception {
		GenerateBpmn gws=new GenerateBpmn();
		String outfile="E:/MyBpmn.xml";
		String path="E:/runtime-EclipseApplication/demo2/控制视图/epc/askForLeave-2011-10-20-02-33-23.epc";
		Epcunits list=ParserEpc.parse(path);
		gws.writeBpmn(outfile,list);



	
	}

}
